//
//  GYMainHeadView.m
//  GoodLuck
//
//  Created by apple on 2018/3/9.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYMainHeadView.h"
#import "GYUser.h"
#import "GYNetWorkTools.h"
#import "GYPictureBrowserView.h"


@interface GYMainHeadView ()<UIScrollViewDelegate>

/** 滑动视图 */
@property (nonatomic, strong) UIScrollView *scrollView;

/** 分页控制器 */
@property (nonatomic, strong) UIPageControl *pageCobtrol;

/** 收藏按钮 */
@property (nonatomic, strong) UIButton *favoritesBtu;
@end

@implementation GYMainHeadView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        //初始化
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI {
    //1.添加子控件
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.bounces = NO;
    
    [self addSubview:scrollView];
    self.scrollView = scrollView;
    
    //增加收藏按钮
    UIButton *btu = [[UIButton alloc] initWithFrame:CGRectMake(self.width-50, self.height-50, 30, 30)];
    btu.layer.cornerRadius = 15;
    btu.layer.borderWidth = 1;
    btu.layer.borderColor = RGBColor(201, 201, 201).CGColor;
    btu.contentEdgeInsets = UIEdgeInsetsMake(6, 6, 6, 6);
    [btu setImage:[UIImage imageNamed:@"favorites_no"] forState:UIControlStateNormal];
    [btu setImage:[UIImage imageNamed:@"favorites"] forState:UIControlStateSelected];
    [btu addTarget:self action:@selector(collectionBtu:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btu];
    self.favoritesBtu = btu;
}

- (void)setFavorate:(BOOL)favorate {
    _favorate = favorate;
    self.favoritesBtu.selected = favorate;
}

- (void)collectionBtu:(UIButton *)sender {
    //1.这里要判断用户有没有登入
    if ([GYUser loadUserInfo] == nil) {
        //跳转到登录界面
        self.loginviewCallBack();
    }else {
        //发送收藏或则取消 该商品的网络请求
        if (sender.selected) {
            //代表已经收藏 发送取消的消息
            [EasyTextView showSuccessText:@"取消收藏成功!"];
            [GYNetWorkTools requestCancelGoodsfavorites:self.goods_id success:^(NSDictionary *obj) {
                NSLog(@"cancelobj====%@",obj);
                int code = [[obj objectForKey:@"code"] intValue];
                NSDictionary *datas = [obj objectForKey:@"datas"];
                if (code == 200) {
                    sender.selected = NO;
                }else if (code == 400) {
                    NSLog(@"加载出错===%@",[datas objectForKey:@"error"]);
                    [EasyTextView showText:@"取消收藏失败!"];
                }
            } error:^(NSError *error) {
                [EasyTextView showText:@"取消收藏失败!"];
                NSLog(@"laodCancelGoodsfavoritesFailed====%@",error);
            }];
            
        }else {
            //代表没有收藏 发送收藏的消息
            [GYNetWorkTools requestAddGoodsfavorites:self.goods_id success:^(NSDictionary *obj) {
                //NSLog(@"addobj======%@",obj);
                int code = [[obj objectForKey:@"code"] intValue];
                NSDictionary *datas = [obj objectForKey:@"datas"];
                if (code == 200) {
                    [EasyTextView showSuccessText:@"收藏成功!"];
                    sender.selected = YES;
                }else if (code == 400) {
                    NSLog(@"加载出错===%@",[datas objectForKey:@"error"]);
                    [EasyTextView showText:@"收藏失败!"];
                }
            } error:^(NSError *error) {
                [EasyTextView showText:@"收藏失败!"];
                NSLog(@"laodAddGoodsfavoritesFailed====%@",error);
            }];
        }
    }
    NSLog(@"收藏按钮点击事件...");
}

//通过分页控制器来控制滑动式图
- (void)changedCurrentPage:(UIPageControl *)pageControl {
    self.scrollView.contentOffset = CGPointMake(ScreenWidth*pageControl.currentPage, 0);
}

- (void)setImages:(NSArray *)images {
    _images = images;
    //设置contenSize
    self.scrollView.contentSize = CGSizeMake(ScreenWidth*images.count, self.height);
    
    //设置分页
    [self.pageCobtrol removeFromSuperview];
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((ScreenWidth-140)/2, self.height-30, 140, 20)];
    pageControl.currentPage = 0;
    pageControl.numberOfPages = images.count;
    pageControl.currentPageIndicatorTintColor = RGBColor(0, 158, 214);//RGBColor(16, 205, 193);
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    [pageControl addTarget:self action:@selector(changedCurrentPage:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:pageControl];
    self.pageCobtrol = pageControl;
    
    
    for (int i = 0; i < images.count; i++) {
        NSURL *url = [NSURL URLWithString:images[i]];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth*i, 0, ScreenWidth, self.height)];
        imageView.tag = 100 + i;
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"4"]];
        imageView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
        [imageView addGestureRecognizer:tap];
        
        [self.scrollView addSubview:imageView];
    }
}

- (void)tapGR:(UITapGestureRecognizer *)tap {
    //[SJAvatarBrowser showImage:(UIImageView *)tap.view];
    //1.首先获取所有图片数据(根据url去缓存中的图片)
    NSMutableArray *allImage = [NSMutableArray array];
    for (NSString *str in self.images) {
        UIImage *cachesImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:str];
        
        [allImage addObject:cachesImage];
    }
    self.imageClickCallBack(allImage);
//
//    GYPictureBrowserView *pictureBrowser = [[GYPictureBrowserView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) images:allImage imageIndex:self.pageCobtrol.currentPage];
//    __weak typeof(self) weakSelf = self;
//    pictureBrowser.showCurrentImageCallBack = ^(NSInteger currentCellIndex) {
//        weakSelf.scrollView.contentOffset = CGPointMake(ScreenWidth*currentCellIndex, 0);
//    };
//    [[UIApplication sharedApplication].keyWindow addSubview:pictureBrowser];
}

#pragma mark -- UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offSetX = scrollView.contentOffset.x;
    self.pageCobtrol.currentPage = offSetX/ScreenWidth;
}
@end
